#pragma once
#include "fxcc/core/graph/common/RenderTargetJack.h"
#include "fxcc/core/graph/opengl3/Texture2DJack.h"
#include "fxcc/core/graph/opengl3/FrameBuffer.h"

namespace fxcc
{
	namespace graph
	{
		namespace opengl3
		{
			struct FXCC_API RenderTargetJack :public fxcc::graph::common::RenderTargetJack
			{
				RenderTargetJack(const fxcc::graph::common::RenderTargetJack::Desc& desc): fxcc::graph::common::RenderTargetJack(desc)
				{
					Load();

				}
				void Load() 
				{
					m_Available = Init();
				}

				bool Init();

				void Begin() const;

				void Viewport() const;

				void End() const;


			public:
		

				fxcc::graph::opengl3::FrameBuffer m_FrameBuffer;

				int GetPickupIndex(int attachment, glm::ivec2 pos) override
				{
					m_FrameBuffer.Bind();
					glReadBuffer(GL_COLOR_ATTACHMENT0 + attachment);

					unsigned char pixel[4];
					glReadPixels(pos.x, pos.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
					return pixel[0] + (pixel[1] << 8) + (pixel[2] << 16);
				};


				int GetPickupIndexAxis(int attachment, glm::vec2 posAxis) override
				{

					const auto& hd = m_Desc.m_MipmapData;

					glm::ivec2 szFloat(hd.m_Width, hd.m_Height);
					glm::ivec2 posFloat(szFloat.x * posAxis.x, szFloat.y * posAxis.y);

					return GetPickupIndex(attachment, posFloat);

				}
				glm::vec4 GetPixelBuffer(int attachment, glm::ivec2 pos)
				{
					m_FrameBuffer.Bind();
					glReadBuffer(GL_COLOR_ATTACHMENT0 + attachment);

					glm::vec4 pixel;
					glReadPixels(pos.x, pos.y, 1, 1, GL_RGBA, GL_FLOAT, &pixel.x);

					return pixel;
				};


				void ClearColor(const glm::vec4& color) override;

				void ClearBuffer(int color, int depthStencil) override;

				glm::vec4 GetPixelBufferAxis(int attachment, glm::vec2 posAxis)
				{
					const auto& hd = m_Desc.m_MipmapData;

					glm::ivec2 szFloat(hd.m_Width, hd.m_Height);
					glm::ivec2 posFloat(szFloat.x * posAxis.x, szFloat.y * posAxis.y);

					return GetPixelBuffer(attachment, posFloat);
				};



			};
		}
	}
}